package HiveUDF;

import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hive.ql.exec.UDF;

public class struct2string extends UDF {
    public String evaluate(List<Long> strList) {
        return strList.toString();
    }

    public String evaluate(List<String> pidList, List<Long> tmList) {
        List<String> list = new ArrayList<>();
        if (pidList.size() + tmList.size() == 0 || pidList.size() != tmList.size())
            return "";
        for (int i = 0; i < pidList.size(); i++) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pid", pidList.get(i));
            jsonObject.put("tm", tmList.get(i));
            list.add(jsonObject.toJSONString());
        }
        return list.toString();
    }

    public String evaluate(List<String> pidList, List<Long> tmList, List<Integer> tpList) {
        List<String> list = new ArrayList<>();
        if (pidList.size() + tmList.size() + tpList.size() == 0 || pidList.size() != tmList.size() || pidList.size() != tpList.size())
            return "";
        for (int i = 0; i < pidList.size(); i++) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pid", pidList.get(i));
            jsonObject.put("tm", tmList.get(i));
            jsonObject.put("tp", tpList.get(i));
            list.add(jsonObject.toJSONString());
        }
        return list.toString();
    }

    public String evaluate(List<Long> priceList, List<Long> tmList, List<String> pidList, List<String> oidList) {
        List<String> list = new ArrayList<>();
        if (pidList.size() + tmList.size() + priceList.size() + oidList.size() == 0 || pidList
                .size() != tmList.size() || pidList
                .size() != priceList.size() || pidList
                .size() != oidList.size())
            return "";
        for (int i = 0; i < pidList.size(); i++) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("price", priceList.get(i));
            jsonObject.put("tm", tmList.get(i));
            jsonObject.put("pid", pidList.get(i));
            jsonObject.put("oid", oidList.get(i));
            list.add(jsonObject.toJSONString());
        }
        return list.toString();
    }
}
